<?php
namespace app\Index\model;

use think\Db;
use think\Model;

class Code extends Model
{
	//添加验证码
	public function get_SmsCode($code)
	{
		if ($code['mobile'] == false) {
			$data = [
				'msg' => '手机号码必填',
				'result' =>	false
			];
			return $data;
		}
		return Db::name('sms_log')->insert($code);
	}

	//查询验证码
	public function get_Code($mobile,$code)
	{
		$where = array();
		$where['mobile'] = $mobile;
		$where['code'] = $code;
		$sms_log = Db::name('sms_log')->where($where)->order('id desc')->find();
		if (!empty($sms_log) && $sms_log['addtime']+60*60 < time()) {
			$data = [
                'msg'   =>  '验证码已过期！',
                'stats' =>  false
            ];
            return $data;
		}
		if ($sms_log['code'] == $code) {
			$data = [
				'msg' => '验证码正确',
				'result' => true
			];
			return $data;
		}else{
			$data = [
				'msg' => '验证码错误',
				'result' => false
			];
			return $data;
		}
	}

	public function getSmsInfo($condition)
	{
		if (empty($condition)) {
            return false;
        }
        $result = Db::name('sms_log')->where($condition)->order('id desc')->find();
        return $result;
	}
}